﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using Cesave.DistributedServices.Core.ErrorHandlers;
using Cesave.DistributedServices.PermisosModule.Resources;
using Cesave.Domain.PermisosModule.Contracts.Estados;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.Infrastructure.CrossCutting.Logging;

namespace Cesave.DistributedServices.PermisosModule
{
	public partial class PermisosModuleService
	{
		#region IPermisosModuleService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Estado> FindEstadosByStatus(bool status = true)
		{
			try
			{
				IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
				return estadoService.FindEstadosByStatus(status);
			}
			catch (ArgumentException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public List<Estado> FindAllEstados()
		{
			try
			{
				IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
				return estadoService.FindAllEstados();
			}
			catch (Exception ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estadoGuid"></param>
		/// <returns></returns>
		public Estado FindEstadoByGuid(Guid estadoGuid)
		{
			try
			{
				IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
				return estadoService.FindEstadoByGuid(estadoGuid);
			}
			catch (ArgumentException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estado"></param>
		public void ChangeEstado(Estado estado)
		{
			try
			{
				IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
				estadoService.ChangeEstado(estado);
			}
			catch (ArgumentException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estado"></param>
		public void ActivateEstado(Estado estado)
		{
			try
			{
				IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
				estadoService.ActivateEstado(estado);
			}
			catch (ArgumentException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estado"></param>
		public void DeactivateEstado(Estado estado)
		{
			try
			{
				IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
				estadoService.DeactivateEstado(estado);
			}
			catch (ArgumentException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		#endregion
	}
}
